package com.haonan.controller;

import com.haonan.pojo.dto.StudentDTO;
import com.haonan.pojo.dto.StudentQueryParam;
import com.haonan.pojo.entily.Result;
import com.haonan.pojo.entily.Student;
import com.haonan.pojo.vo.PageResult;
import com.haonan.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping
    public Result QueryParam(StudentQueryParam studentQueryParam){
        PageResult<Student> page=studentService.param(studentQueryParam);
        return Result.success(page);
    }
    @PostMapping
    public Result insert(@RequestBody StudentDTO studentDTO){
        studentService.insert(studentDTO);
        return Result.success();
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable String ids){
        String[] data=ids.split(",");
        List<Integer> list=new ArrayList<>();
        for (String s : data) {
            list.add(Integer.parseInt(s));
        }
        studentService.deleteById(list);
        return Result.success();
    }

    @GetMapping("/{id}")
        public Result findById(@PathVariable("id") Integer id){
        Student student=studentService.findById(id);
        return Result.success(student);
    }

    @PutMapping
    public Result update(@RequestBody StudentDTO studentDTO){
        studentService.update(studentDTO);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable("id") Integer id,@PathVariable("score") Short score){
        studentService.updateViolation(id,score);
        return Result.success();
    }


}
